-
-
Notifications
You must be signed in to change notification settings - Fork 32.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[material-ui][TablePagination] Fix type error in Select props #39137
[material-ui][TablePagination] Fix type error in Select props #39137
Conversation
Netlify deploy previewhttps://deploy-preview-39137--material-ui.netlify.app/ Bundle size report |
4e01164
to
69b3189
Compare
Thanks for the PR. Please take a look at the failing CI checks. Also, please rebase on the latest master (this should help with the failing Argos check). |
2c9d6d2
to
fe22382
Compare
@michaldudak Thanks! I made some more changes that I think solves the issue well. Should I create some tests for PR? After my rebase I get this error however, which I find confusing since I have not altered any files in either mui-base or the corresponding component. Do you have any idea how I can solve it? \GolandProjects\material-ui\packages\mui-base\src\useTabPanel\useTabPanel.test.js |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@PaulKristoffersson The type changes based on the variant should be made within the Select component types themselves, specifically in Select.d.ts
file. This issue pertains to the SelectProps
.
The type tests in the CI are still failing.
After my rebase I get this error however, which I find confusing since I have not altered any files in either mui-base or the corresponding component. Do you have any idea how I can solve it?
\GolandProjects\material-ui\packages\mui-base\src\useTabPanel\useTabPanel.test.js
4:51 error Unable to resolve path to module 'test/utils' import/no-unresolved
This is because test utilities are no longer imported from test/utils. To resolve this locally, please run yarn install
from the project's root directory.
Let me know if you need any help.
fe22382
to
3434dc6
Compare
@ZeeshanTamboli Thanks for the answer! Are you sure that I am supposed to the values inside the Select.d.ts file? Based on the reffered pull request (#36737) in the issue (#38487) I assumed that we wanted to assert our types based on the given Select variant inside of the component. By doing this we can pass the correct type of InputProps to the Select component and avoid the typeError. Could you provide some additional information on how you would want that implemented inside of the Select component itself? |
@PaulKristoffersson Since TablePagination uses SelectProps of the Select component, I think it makes sense to fix it in the Select component types itself. As an example, I think when the Select component has <Select variant="filled" hiddenLabel /> ---> Type error for hiddenLabel
We want the type to be based on the select variant. I didn't understand what you mean by the Can you confirm the fix by adding TypeScript tests? |
@ZeeshanTamboli Hey, I just came back to this issue after having worked on some other ones, sorry for the delay. In response to: "I think it makes sense to fix it in the Select component types itself. As an example, I think when the Select component has filled variant, hiddenLabel prop should be available:" As far as I can tell Select does not contain the hiddenLabel prop, instead the hiddenLabel prop is contained in one of the variations of the inputProps prop, namely FilledInput. Do you want me to add hiddenLabel as a prop? Otherwise I can try to add some tests to show how my solution works. |
3434dc6
to
7430fc8
Compare
@ZeeshanTamboli Just noticed that the SelectProps prop is deprecated and we slotProps.select instead, so I assume that there is not point on working with this issue anymore? |
It doesn't have the
But the |
7430fc8
to
0bf7996
Compare
@ZeeshanTamboli Thank you for the answer! I moved my changes to slotProps, and instead of focusing on InputProps I now extend the SelectProps with the different props based on the Select variant. I get no type errors with the component in my local playground with my component as such:
Do you have any suggestion on what test I should make to confirm the behaviour? |
bd61932
to
1fa9eb1
Compare
@ZeeshanTamboli I have added a test now as well. If you have any inputs on it they are very welcome :) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This solution functions correctly, but these changes should be made in the Select component's type definitions instead (file Select.d.ts
). This is where SelectProps
is defined, addressing the root issue. Both TablePagination and TextField utilize SelectProps
. Importing input-related components (FilledInput, OutlinedInput) in TablePagination doesn't align logically.
packages/mui-material/src/TablePagination/TablePagination.test.js
Outdated
Show resolved
Hide resolved
096a693
to
e50fb50
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you include the test for issue #38487 in the TablePagination.spec.tsx
file?
Added the test 👍 |
export type SelectProps< | ||
Value = unknown, | ||
Variant extends SelectVariants = SelectVariants, | ||
> = BaseSelectProps<Value> & | ||
(Variant extends 'filled' | ||
? FilledSelectProps | ||
: Variant extends 'standard' | ||
? StandardSelectProps | ||
: OutlinedSelectProps); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FYI, this is breaking type change for people using SelectProps
directly because the variant
prop is required now when it wasn't before. I guess BaseSelectProps
is a suitable replacement for most use cases?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! Using BaseSelectProps
instead of SelectProps
fixed the build in our component library after updating mui to 5.15.11
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed it in #41359
The switch from interface to type also breaks for anyone extending it.
|
Do you have any suggestions on how to resolve this? It is breaking our CI. |
Hey everyone! I'm sorry for any trouble this might have caused. As an immediate workaround, if you were using Unfortunately, I don't think that would work for @damisparks case. I'll take a look into that. I'll get back to you as soon as possible. |
@damisparks in the meantime, this workaround might help import {
Select as MaterialSelect,
SelectProps as MaterialSelectProps,
SelectVariants as MaterialSelectVariants,
} from '@mui/material';
type SelectProps<Value, Variant extends MaterialSelectVariants> = { variant?: Variant } & Omit<
MaterialSelectProps<Value, Variant>,
'variant'
>;
export default function Select<Value, Variant extends MaterialSelectVariants>(
props: SelectProps<Value, Variant>,
) {
return <MaterialSelect<Value, Variant> {...props} />;
} |
@DiegoAndai I tried the |
@DiegoAndai, I appreciate the workaround. It helped to resolve the TS errors. The final implementation looks like this. import MuiSelect, {
type SelectProps as MuiSelectProps,
type SelectVariants as MuiSelectVariants,
} from "@mui/material/Select";
import copyComponentStaticProps from "../copyComponentStaticProps.js";
// The function `wrapComponent` cannot be used because this component has a
// generic type parameter.
/**
* A [Material UI](https://mui.com/material-ui) React component for a select
* input.
*
* - [Material UI guide](https://mui.com/material-ui/react-select)
* - [Material UI API docs](https://mui.com/material-ui/api/select)
* @param props Props.
* @returns Select JSX.
*/
function Select<Value, Variant extends MuiSelectVariants>(
props: {
variant?: Variant;
} & Omit<MuiSelectProps<Value, Variant>, "variant">,
) {
return <MuiSelect<Value, Variant> {...props} />;
}
copyComponentStaticProps(MuiSelect, Select);
export default Select; |
I would expect this to work: import { Select as MaterialSelect, SelectProps, SelectVariants } from '@mui/material';
export function MySelect<Value, Variant extends SelectVariants>(
props: SelectProps<Value, Variant>,
) {
return <MaterialSelect<Value, Variant> {...props} />;
} But it isn't. Maybe I'm misunderstanding something. Or perhaps it is related to microsoft/TypeScript#10571 / microsoft/TypeScript#23696 / microsoft/TypeScript#26242. If someone figures out why it isn't working, please reach out 😊. I'll bring this topic to the MUI Core team to gather ideas. |
@DiegoAndai, Should I raise an issue for this topic? Otherwise, I am afraid the conversation will get lost under this PR. |
@DiegoAndai Even though the code below fits the TS errors. import MuiSelect, {
type SelectProps as MuiSelectProps,
type SelectVariants as MuiSelectVariants,
} from "@mui/material/Select";
function Select<Value, Variant extends MuiSelectVariants>(
props: {
variant?: Variant;
} & Omit<MuiSelectProps<Value, Variant>, "variant">,
) {
return <MuiSelect<Value, Variant> {...props} />;
}
copyComponentStaticProps(MuiSelect, Select); It does not address choosing a concrete variant for the second generic argument. At the same time, the story (i.e.) Conceptually, MUI has combined multiple components with different props, HTML elements and styles into one It makes sense that the storybook cannot generate the correct props documentation and controls for the MUI |
@damisparks yes please! Let's open a new issue to continue this discussion. Tag me on it as well. Please add a live reproduction that demonstrates the typescript error. You can also add a live reproduction for the storybook case. |
@damisparks Someone else opened a related issue, but I think it's not quite the same, so let's open yours as well. |
@DiegoAndai, thanks for the update. I will raise an issue and provide minimal storybook case reproduction. |
Hey everyone, we've been discussing this PR's implementation and changes in #41405 as it introduced some regressions. We're thinking of modifying it. If you're interested, please take a look and join the conversation. |
As of mui/material-ui#39137, MUI's `SelectProps` is now a `type` and not an `interface`, so we can no longer `extend` it for our own `MenuSelectProps`. As such, we now make `MenuSelectProps` a `type` and use `&` syntax to inject additional props. This should support both MUI < v5.15.11 (where `SelectProps` is an `interface`) and >= 5.15.11 (where `SelectProps` is a `type`).
As of mui/material-ui#39137, MUI's `SelectProps` is now a `type` and not an `interface`, so we can no longer `extend` it for our own `MenuSelectProps`. As such, we now make `MenuSelectProps` a `type` and use `&` syntax to inject additional props. This should support both MUI < v5.15.11 (where `SelectProps` is an `interface`) and >= 5.15.11 (where `SelectProps` is a `type`).
Closes #38487
I based this solution on the changes made in #36737
Compared to #36737 I assumed we provide the TablePaginationOwnProps no matter which variant is used, since no other variant of props was defined beforehand.